// RadiusAuthTest.cpp: implementation of the CRadiusAuthTest class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AgentPlugin.h"
#include "RadiusAuthTest.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

extern CAgentPluginApp theApp;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRadiusAuthTest::CRadiusAuthTest()
{

}

CRadiusAuthTest::~CRadiusAuthTest()
{

}

void CRadiusAuthTest::LoadConfiguration()
{
	CRadiusAuth::LoadConfiguration();

	CString strTestedUsers;
		::GetPrivateProfileString( theApp.m_strIniAppName, "TestedUsers", "", strTestedUsers.GetBuffer(m_iMaxIniItemLen), m_iMaxIniItemLen, theApp.m_strIniFilePath );
		strTestedUsers.ReleaseBuffer();
	m_arrTestedUsers.AppendItems( strTestedUsers );
}

long CRadiusAuthTest::Authenticate(const char *const pszUserName, const char *const pszPassword, const long lTimeOut)
{
	if (pszUserName == theApp.m_pRadiusAuth->m_strDomainAdmin)
	{
#ifdef _DEBUG
		CString strMsg;
			strMsg.Format( "Non-extended:\t%s(%s)", pszUserName, pszPassword );
		theApp.Log( strMsg );
#endif
		return PAM_DEFAULT;
	}

	CStringItemsArray arrBeErrorUsers;
		CString strBeErrorUsers;
			::GetPrivateProfileString( theApp.m_strIniAppName, "TestedUsers", "", strBeErrorUsers.GetBuffer(m_iMaxIniItemLen), m_iMaxIniItemLen, theApp.m_strIniFilePath );
			strBeErrorUsers.ReleaseBuffer();
			arrBeErrorUsers.AppendItems( strBeErrorUsers );
	for (int iErrUser= 0; iErrUser < m_arrTestedUsers.GetSize(); iErrUser++)
	{
		if (pszUserName == m_arrTestedUsers[iErrUser])
			return (PAM_ERROR);
	}

	for (int i= 0; i < m_arrTestedUsers.GetSize(); i++)
	{
		if (pszUserName == m_arrTestedUsers[i])
			return (CRadiusAuth::Authenticate( pszUserName, pszPassword, lTimeOut ));
	}

	return (PAM_DEFAULT);
}
